//
//  XFRefreshDownloadView.m
//  WorkManager
//
//  Created by ukongm on 17/1/8.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFRefreshDownloadView.h"
#import "XFManager.h"
@interface XFRefreshDownloadView()

/** 线宽 */
@property(nonatomic,assign)CGFloat lineWidth;

@end

@implementation XFRefreshDownloadView
{
    dispatch_source_t _timer;
}
- (CGFloat)lineWidth{
    if (_lineWidth == 0) return 3;
    
    return _lineWidth;
}

- (instancetype)initWithFrame:(CGRect)frame andLineWidth:(CGFloat)lineWidth
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.lineWidth = lineWidth;
        
        [self createLayer];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createLayer];
    }
    return self;
}

- (void)createTimer{
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(_timer, ^{
        
        static CGFloat curRotation = 0;
        CGFloat speed = 0.1;
        curRotation += speed;
        CGFloat rotation = (M_PI * (curRotation));
        
        self.transform = CGAffineTransformMakeRotation(rotation);
        
    });
}

- (void)createLayer{
    
    self.hidden = YES;
    
    CAShapeLayer* shapeLayer = [CAShapeLayer layer];
    
    CGFloat lineWith = self.lineWidth;
    
    UIBezierPath* path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(lineWith, lineWith, self.width - lineWith * 2, self.height - lineWith * 2)];
    
    shapeLayer.path = path.CGPath;
    shapeLayer.strokeColor = [UIColor arcColor].CGColor;
    shapeLayer.strokeStart = 0;
    shapeLayer.strokeEnd = 1;
    shapeLayer.lineWidth = lineWith;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    
    CAGradientLayer* gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    gradientLayer.colors = [UIColor rainbowColors];
//    gradientLayer.locations = @[@0.5,@1];
    gradientLayer.startPoint = CGPointZero;
    gradientLayer.endPoint = CGPointMake(1, 1);
    gradientLayer.mask = shapeLayer;
    
//    CALayer *gradientLayer = [CALayer layer];
//    CAGradientLayer *gradientLayer1 =  [CAGradientLayer layer];
//    gradientLayer1.frame = CGRectMake(0, 0, self.width/2, self.height);
//    [gradientLayer1 setColors:[UIColor rainbowColors]];
////    [gradientLayer1 setLocations:@[@0.5,@0.9,@1 ]];
//    [gradientLayer1 setStartPoint:CGPointMake(0.5, 1)];
//    [gradientLayer1 setEndPoint:CGPointMake(0.5, 0)];
//    [gradientLayer addSublayer:gradientLayer1];
//    
//    CAGradientLayer *gradientLayer2 =  [CAGradientLayer layer];
////    [gradientLayer2 setLocations:@[@0.1,@0.5,@1]];
//    gradientLayer2.frame = CGRectMake(self.width/2, 0, self.width/2, self.height);
//    [gradientLayer2 setColors:[UIColor rainbowColors]];
//    [gradientLayer2 setStartPoint:CGPointMake(0.5, 0)];
//    [gradientLayer2 setEndPoint:CGPointMake(0.5, 1)];
//    [gradientLayer addSublayer:gradientLayer2];
    
    gradientLayer.mask = shapeLayer;
    
    [self.layer addSublayer:gradientLayer];
}

- (void)startAnimation{
    self.hidden = NO;
    [self createTimer];
    dispatch_resume(_timer);
}

- (void)stopAnimation{
    self.hidden = YES;
    if (_timer) {
        dispatch_cancel(_timer);
    }
}

@end
